﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace BTN04.DTO
{
    public class GioHang
    {
        readonly List<CartItem> _item;

        public List<CartItem> Item
        {
            get { return _item; }
        }
        public GioHang()
        {
            _item = new List<CartItem>();
        }

        public void AddItem(CartItem item)
        {
            CartItem hang = new CartItem();
            hang = _item.SingleOrDefault(c=>c.Masp ==item .Masp );
            if (hang != null)
            {
                hang.SoLuong += item.SoLuong;
            }
            else
            {
                _item.Add(item);
            }
        }
        public void UpdateItem(int masp, int soluong)
        {
            CartItem itemUpdate = _item.Single(c => c.Masp == masp);
            itemUpdate.SoLuong = soluong;
        }

        public void RemoveItem(int masp)
        {
            CartItem itemDelete = _item.Single(c=>c.Masp ==masp );
            _item.Remove(itemDelete);
        }

        public void HuyGioHang()
        {
            for (int i = _item.Count-1 ; i>=0; i--)
            {
                _item.RemoveAt(i);
            }

        }

        public int SoLuongHang()
        {
            return _item.Sum(c => c.SoLuong);
        }

        public int LaySoLuongTheoMa(int masp)
        {
            int sl=0;
            for (int i = 0; i <_item .Count ; i++)
            {
                if (_item[i].Masp == masp)
                {
                    sl = _item[i].SoLuong;
                    break;
                }
            }
            return sl;
        }

        //Hàm xoa sản phẩm có số lượng băng 0
        public void XoaSanPham()
        {
            for (int i = _item.Count - 1; i >= 0; i--)
            {
                if(_item [i].SoLuong ==0)
                    _item.RemoveAt(i);
            }
        }
    }
}
